//
//  BIDVersionViewController.m
//  cxgov
//
//  Created by mal on 16/3/16.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDVersionViewController.h"
#import <PgyUpdate/PgyUpdateManager.h>

@interface BIDVersionViewController ()<UIAlertViewDelegate>
{
    NSString *_curVersion;
    NSString *_downloadURL;
}
@end

@implementation BIDVersionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"检查更新";
    // Do any additional setup after loading the view from its nib.
    _containerView1.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _containerView1.layer.borderWidth = 1.f;
    _containerView1.layer.cornerRadius = 5.f;
    //
    _containerView2.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _containerView2.layer.borderWidth = 1.f;
    _containerView2.layer.cornerRadius = 5.f;
    //
    _containerView3.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _containerView3.layer.borderWidth = 1.f;
    _containerView3.layer.cornerRadius = 5.f;
    //
    _updateBtn.layer.cornerRadius = 5.f;
    _updateBtn.clipsToBounds = YES;
    //
    //获取当前版本
    _curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    _versionLabel.text = [[NSString alloc] initWithFormat:@"V %@", _curVersion];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)updateBtnHandler:(id)sender
{
    [[PgyUpdateManager sharedPgyManager] checkUpdateWithDelegete:self selector:@selector(pgyUpdateHandler:)];
}

- (void)pgyUpdateHandler:(NSDictionary*)infoDic
{
    if(!ISNULL(infoDic))
    {
        NSString *versionName = infoDic[@"versionName"];
        _downloadURL = infoDic[@"downloadURL"];
        NSString *strMsg = [[NSString alloc] initWithFormat:@"当前版本:%@, 最新版本:%@, 是否更新", _curVersion, versionName];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:strMsg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
        alertView.tag = 1;
        [alertView show];
    }
    else
    {
        [BIDGlobalMethods showAlertView:@"当前已经是最新版本了" buttonTitle:@"关闭" delegate:nil tag:0];
    }
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==1)
    {
        if(buttonIndex==1)
        {
            [[PgyUpdateManager sharedPgyManager] updateLocalBuildNumber];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_downloadURL]];
        }
    }
}

@end
